package com.thoughtworks;

public class Triangle {
    private static final char DEFAULT_CHARACTER = '*';

    private char character;

    public Triangle() {
        character = DEFAULT_CHARACTER;
    }

    public Triangle(char characterOverride) {
        character = characterOverride;
    }

    public String getHorizontalLine(int size) {
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < size; i++) {
            stringBuilder.append(character);
        }

        return stringBuilder.toString();
    }

    public String getVerticalLine(int size) {
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < size; i++) {
            stringBuilder.append(character + "\n");
        }

        return stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
    }

    public String getRightTriangle(int size) {
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 1; i < size + 1; i++) {
            stringBuilder.append(getHorizontalLine(i) + '\n');
        }

        return stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
    }

    public String getIsoscelesTriangle(int size) {
        StringBuilder stringBuilder = new StringBuilder();
        Triangle spacer = new Triangle(' ');

        for (int i = 1; i < size + 1; i++) {
            stringBuilder.append(spacer.getHorizontalLine(size - i));
            stringBuilder.append(getHorizontalLine(i * 2 - 1) + '\n');
        }

        return stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
    }

    public String getDiamond(int size) {
        StringBuilder stringBuilder = new StringBuilder();
        Triangle spacer = new Triangle(' ');

        stringBuilder.append(getIsoscelesTriangle(size));
        for (int i = size - 1; i > 0; i--) {
            stringBuilder.append('\n' + spacer.getHorizontalLine(size - i));
            stringBuilder.append(getHorizontalLine(i * 2 - 1));
        }

        return stringBuilder.toString();
    }

    public String getDiamondWithName(int size, String name) {
        String lineToReplace = getHorizontalLine(size * 2 - 1);
        return getDiamond(size).replace(lineToReplace, name);
    }

    public static void main(String[] args) {
        Triangle triangle = new Triangle();
        System.out.println(triangle.getHorizontalLine(1));
        System.out.println(triangle.getHorizontalLine(8));
        System.out.println(triangle.getVerticalLine(3));
        System.out.println(triangle.getRightTriangle(3));
        System.out.println(triangle.getIsoscelesTriangle(3));
        System.out.println(triangle.getDiamond(3));
        System.out.println(triangle.getDiamondWithName(3, "Bill"));
    }
}